// pages/search/search.js
let {
  getSearch
} = require("../../api/search")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 搜索数组
    searchList: [{
        id: 1,
        name: "小米",
      },
      {
        id: 2,
        name: "电视",
      },
      {
        id: 3,
        name: "手机",
      },
      {
        id: 4,
        name: "空调",
      },
      {
        id: 5,
        name: "充电器",
      },
      {
        id: 6,
        name: "耳机",
      },
    ],
    flag: false,
    lists: [],
    tab_list: [],
    valueInp: "",
    value_normal: "",
    conIndex: -1,
    flag: false,
    //橙色字体显示
    tab_show: 1,
    normal_lists: [],
    normal_value: "",
    price_show: true,
    sales: true,
    history_lists: [],
    flagId: 0

  },
  getInputValue(e) {
    console.log(e.detail.value);
    this.setData({
      "valueInp": e.detail.value,
      'value_normal': e.detail.value,
    })
  },
  searchValue(event) {
    console.log("是");
    let name = event.currentTarget.dataset.name
    this.setData({
      valueInp: name
    })
  },
  // 搜索内容
  SearchName() {
    console.log("是");
    let value = this.data.valueInp
    let fla = this.data.flag
    this.data.history_lists.push(value);
    this.setData({
      flag: !fla
    })
    getSearch({
      "query": value
    }).then(data => {
      console.log(data.data);
      this.setData({
        "lists": data.data.list_v2,
        "tab_list": data.data.tab_classes,
        "valueInp": "",
        "normal_lists": data.data.list_v2,
        'history_lists': this.data.history_lists,
      })
      wx.setStorageSync('history_lists', this.data.history_lists);
      if (this.data.lists.length) {
        this.setData({
          flag: fla
        })
      }
    })
    // setTimeout(function () {
    //   console.log("触发定时器");
    //   if (!this.data.lists.length) {
    //     this.setData({
    //       flag: fla

    //     })
    //   }
    //   console.log('')
    // }, 8000); // 2000为毫秒级参数，表示2秒
  },
  //删除历史记录
  delHistory() {
    wx.removeStorageSync('history_lists');
    this.setData({
      'history_lists': []
    })
  },
  setConIndex(event) {
    console.log(event.currentTarget,'11');
    console.log(event.currentTarget.dataset.id);
    let index = event.currentTarget.dataset.id
    let name = event.currentTarget.dataset.name
    this.setData({
      "conIndex": index,

    })
    getSearch({
      "query": name
    }).then(data => {
      console.log(data.data);
      this.setData({
        "lists": data.data.list_v2,
      })
    })

  },

  //切换到综合
  toggle_normal() {
    // console.log(111,this.data.value_normal);
    getSearch({
      "query": this.data.value_normal
    }).then(data => {
      this.setData({
        "lists": data.data.list_v2,
        "tab_show": 1,
        'price_show': true,
        'sales': true,

        flagId: 0
      })
    })
  },

  //切换销量
  toggle_count() {
    //销量从高到低
    if (this.data.sales) {
      var max;
      for (var i = 0; i < this.data.lists.length; i++) {
        for (var j = i; j < this.data.lists.length; j++) {
          if (parseInt(this.data.lists[i].body.comments_total) < parseInt(this.data.lists[j].body.comments_total)) {
            max = this.data.lists[j];
            this.data.lists[j] = this.data.lists[i];
            this.data.lists[i] = max;
          }
        }
      }
      this.setData({
        "lists": this.data.lists,
        "tab_show": 2,
        'sales': false,
        'price_show': true,
        flagId: 0
      })
    } else {
      var min;
      for (var i = 0; i < this.data.lists.length; i++) {
        for (var j = i; j < this.data.lists.length; j++) {
          if (parseInt(this.data.lists[i].body.comments_total) > parseInt(this.data.lists[j].body.comments_total)) {
            min = this.data.lists[j];
            this.data.lists[j] = this.data.lists[i];
            this.data.lists[i] = min;
          }
        }
      }
      this.setData({
        "lists": this.data.lists,
        "tab_show": 2,
        'sales': true,
      })
    }

  },

  //切换价格
  toggle_price() {
    if (this.data.price_show) {
      var max_price;
      for (var i = 0; i < this.data.lists.length; i++) {
        for (var j = i; j < this.data.lists.length; j++) {
          if (parseInt(this.data.lists[i].body.price) < parseInt(this.data.lists[j].body.price)) {
            max_price = this.data.lists[j];
            this.data.lists[j] = this.data.lists[i];
            this.data.lists[i] = max_price;
          }
        }
      }
      this.setData({
        "lists": this.data.lists,
        "tab_show": 3,
        'price_show': false,
        flagId: 1,
        'sales': true,

      })
    } else {
      var min_price;
      for (var i = 0; i < this.data.lists.length; i++) {
        for (var j = i; j < this.data.lists.length; j++) {
          if (parseInt(this.data.lists[i].body.price) > parseInt(this.data.lists[j].body.price)) {
            min_price = this.data.lists[j];
            this.data.lists[j] = this.data.lists[i];
            this.data.lists[i] = min_price;
          }
        }
      }
      this.setData({
        "lists": this.data.lists,
        "tab_show": 3,
        'price_show': true,
        flagId: 2
      })
    }


  },
  //切换评价
  // toggle_satisfy_per() {
  //   var max_satisfy;
  //   for (var i = 0; i < this.data.lists.length; i++) {
  //     for (var j = i; j < this.data.lists.length; j++) {
  //       if (parseInt(this.data.lists[i].body.satisfy_per.slice(0, this.data.lists[i].body.satisfy_per.length - 1)) * 10 < parseInt(this.data.lists[j].body.satisfy_per.slice(0, this.data.lists[j].body.satisfy_per.length - 1)) * 10) {
  //         console.log(parseInt(this.data.lists[i].body.satisfy_per.slice(0, this.data.lists[i].body.satisfy_per.length - 1)) * 10);
  //         max_satisfy = this.data.lists[j];
  //         this.data.lists[j] = this.data.lists[i];
  //         this.data.lists[i] = max_satisfy;
  //       }
  //     }
  //   }
  //   this.setData({
  //     "lists": this.data.lists,
  //     "tab_show": 4,
  //     'price_show': true,
  //     flagId: 0,
  //     'sales': true,
  //   })
  // },
  toggle_satisfy_per() {
    //销量从高到低
    if (this.data.sales) {
      var max;
      for (var i = 0; i < this.data.lists.length; i++) {
        for (var j = i; j < this.data.lists.length; j++) {
          if (parseInt(this.data.lists[i].body.comments_total) < parseInt(this.data.lists[j].body.comments_total)) {
            max = this.data.lists[j];
            this.data.lists[j] = this.data.lists[i];
            this.data.lists[i] = max;
          }
        }
      }
      this.setData({
        // "lists": this.data.lists,
        // "tab_show": 4,
        // 'sales': false,
        // 'price_show': true,
        // flagId: 0
        "lists": this.data.lists,
        "tab_show": 4,
        'sales': false,
        'price_show': true,
        flagId: 0
      })
    } else {
      var min;
      for (var i = 0; i < this.data.lists.length; i++) {
        for (var j = i; j < this.data.lists.length; j++) {
          if (parseInt(this.data.lists[i].body.comments_total) > parseInt(this.data.lists[j].body.comments_total)) {
            min = this.data.lists[j];
            this.data.lists[j] = this.data.lists[i];
            this.data.lists[i] = min;
          }
        }
      }
      this.setData({
        "lists": this.data.lists,
        "tab_show": 4,
        'sales': true,
      })
    }

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options.name);
    let value = options.name
    this.setData({
        'normal_value': value
      }),
      getSearch({
        "query": value
      }).then(data => {
        this.setData({
          "lists": data.data.list_v2,
          "tab_list": data.data.tab_classes,
        })
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      'history_lists': wx.getStorageSync('history_lists') ? wx.getStorageSync('history_lists') : [],
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})